package com.min.dom;

import org.dom4j.*;
import org.dom4j.dom.DOMCDATA;
import org.dom4j.dom.DOMElement;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Iterator;

/**
 * Created by llx on 2016/12/11.
 */
public class DomUtils {

    public static void main(String[] args) throws DocumentException {
        //创建一个xml解析器对象
        SAXReader reader = new SAXReader();
        Document doc = reader.read(new File("src/main/resources/contact.xml"));
        Element element = doc.getRootElement();
        String rootName = element.getName();
        System.out.println(doc.getPath());

        Iterator<Node> iterator = doc.nodeIterator();
        while (iterator.hasNext()){
            Node node = iterator.next();
            System.out.println(node.getName());
        }

        System.out.println(rootName);
        System.out.println(doc);
        System.out.println("----------------------------");
        getElement(element);
        System.out.println("----------------------------");
        System.out.println(element.getTextTrim());
        System.out.println("----------------------------");
        element.add(new DOMElement("ages"));

        try {
            writeDocument();
            updateDomcument();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void getElement(Element element){
        Iterator<Node> node = element.nodeIterator();
        while (node.hasNext()){
            Node no = node.next();
            if(no instanceof Element){
                System.out.println(((Element) no).attribute("id"));
                getElement((Element) no);
            }
        }
    }

    public static void writeDocument() throws Exception {

        Element root = new DOMElement("grils");
        Document doc = DocumentHelper.createDocument(root);

        OutputFormat format1 = OutputFormat.createCompactFormat();//紧凑格式
        OutputFormat format2 = OutputFormat.createPrettyPrint();//漂亮格式
        format1.setEncoding("utf-8");
        format2.setEncoding("utf-8");
        FileOutputStream out = new FileOutputStream(new File("src/main/resources/girl.xml"));
        XMLWriter writer = new XMLWriter(out,format2);

        Element gril = new DOMElement("girl");
        gril.addAttribute("id","001");
        gril.addAttribute("class","grilClass");

        Element age = new DOMElement("age");
        age.addAttribute("id","ageId");
        age.addText("24");
        gril.add(age);

        Element name = new DOMElement("name");
        name.addText("miner");
        gril.add(name);

        Element realName = new DOMElement("realName");
        realName.addAttribute("id","realName");
        realName.addText("赵丽颖");
        realName.add(new DOMCDATA("<>"));

        name.add(realName);
        root.add(gril);

        writer.write(doc);
        writer.flush();
        writer.close();

    }

    public static void updateDomcument() throws Exception {

        Document doc = new SAXReader().read("src/main/resources/girl.xml");

        System.out.println("<><><><><><><><><><><><><><><><><>");
        doc.getRootElement().element("girl").element("name").setText("萤火虫");
        doc.getRootElement().element("girl").element("name").element("realName").detach();
        XMLWriter writer = new XMLWriter(new FileOutputStream("src/main/resources/girl.xml"));
        writer.write(doc);
        writer.flush();
        writer.close();

    }

    public static void xpath(){



    }

}
